package com.hrt.web.comments.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.hrt.base.BaseDaoImpl;
import com.hrt.bean.Comments;
import com.hrt.web.comments.dao.CommentsWebDao;

@Repository
public class CommentsWebDaoImpl extends BaseDaoImpl<Comments> implements CommentsWebDao{
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Comments> findDelAll() {
		return getHibernateTemplate().find("FROM Comments WHERE isdelete=0");
	}
	
	/**分页查询的重写*/
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<Comments> queryPageByGiftId(List conditions,
			int pageIndex, int pageSize) {
		String hql = "select comm from Comments comm";
		Map delcon = new HashMap();
		delcon.put("field", "isdelete");
		delcon.put("value", 0);
		conditions.add(delcon);
		for (int i = 0; i < conditions.size(); i++) {
			Map con = (Map) conditions.get(i);
			String field = con.get("field").toString();
			String val = con.get("value").toString();
			if (i == 0) {
				hql = hql + " where comm." + field + " in(" + val+")";
			} else {
				hql = hql + " and comm." + field + "=" + val+" ORDER BY id DESC";
			}
		}
		List<Comments> results = super.queryByPage(hql, pageIndex, pageSize);
		return results;
	}
	
	/**追评的分页*/
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<Comments> queryAddCommById(List conditions,
			int pageIndex, int pageSize) {
		String hql = "select comm from Comments comm";
		Map delcon = new HashMap();
		delcon.put("field", "isdelete");
		delcon.put("value", 0);
		conditions.add(delcon);
		for (int i = 0; i < conditions.size(); i++) {
			Map con = (Map) conditions.get(i);
			String field = con.get("field").toString();
			String val = con.get("value").toString();
			if (i == 0) {
				hql = hql + " where comm." + field + " in(" + val+")";
			} else {
				hql = hql + " and comm." + field + "=" + val+" and comm.isAddComment=1 ORDER BY id DESC";
			}
		}
		List<Comments> results = super.queryByPage(hql, pageIndex, pageSize);
		return results;
	}

	/**根据礼品规格Id查找评论Id*/
	@SuppressWarnings("unchecked")
	@Override
	public List<Long> findCommIdByGdeId(String gdeIds) {
		return getHibernateTemplate().find("SELECT  gcomm.id FROM Comments gcomm WHERE gcomm.isdelete=0 AND gcomm.gdeId IN ("+gdeIds+")");
	}

	/**根据规格id获取数量*/
	@Override
	public Long findNumByGdeId(String gdeIds) {
		return (long) getHibernateTemplate().find("select comm from Comments comm where comm.id in("+gdeIds+") and comm.isdelete=0 ORDER BY id DESC").size();
	}
	
	/**追评   --》根据规格id获取数量*/
	@Override
	public Long findAddCommNumByGdeId(String gdeIds) {
		return (long) getHibernateTemplate().find("select comm from Comments comm where comm.id in("+gdeIds+") and comm.isdelete=0 and comm.isAddComment=1 ORDER BY id DESC").size();
	}
}
